using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.AspNetCore.SignalR.Client;

namespace SignalRClient;

[ObservableObject]
public partial class MainPageViewModel {
    [ObservableProperty] private string _input;

    [ObservableProperty] private string _output;

    private HubConnection _connection;

    [RelayCommand]
    private async Task ConnectAsync() {
        _connection = new HubConnectionBuilder()
            .WithUrl("http://localhost:5267/hub").Build();
        _connection.On<string>("EchoMessage",
            (message) => { Output = message; });
        await _connection.StartAsync();
    }

    [RelayCommand]
    private async Task SendAsync() {
        await _connection.SendAsync("Echo", Input);
    }
}